const Utils = require("./Utils.js")

Utils.init()

var flag, one_flag, count, num, region, seconds

count = 0
num = 0
seconds = 0
region = [1000, 300, 1600, 800]
flag = true
one_flag = true

// 关闭好友协助申请
threads.start(() => {
  while (true) {
    sleep(30 * 1000)
    Utils.findPhotoTap("叉号", 0.8, [1750, 160, 160, 160])
  }
})

while (flag) {
  Utils.sleeps()
  if (
    Utils.findPictureTap("勋章空", region) ||
    Utils.findPictureTap("勋章实", region)
  ) {
    while (one_flag) {
      if (Utils.findPictureTap("奖励")) {
        count += 1
        one_flag = false
        /********** 突破刷新点击领取奖励 start **********/
        for (let i = 0; i < 3; i++) {
          Utils.sleeps()
          if (Utils.findPictureTap("奖励")) {
            break
          }
        }
        /********** 突破刷新点击领取奖励 end **********/
      } else if (Utils.findPictureTap("失败")) {
        one_flag = false
      } else {
        if (Utils.findPictureTap("进攻", 0.8)) {
          Utils.waitTime(8)
          if (Utils.findPicture("进攻", 0.8)) {
            log("结界挑战券不足")
            Utils.stopJB()
          }
          // 特殊活动时不自动准备放开
        //   do {
        //     Utils.findPictureTap("准备")
        //     sleep(1000)
        //   } while (!Utils.findPicture("自动"))
          seconds = random(20, 30)
          Utils.waitTime(seconds)
        }
        log("已等待：" + (seconds += 2) + "秒")
      }
    }
    one_flag = true
    num = 0
    seconds = 0
  } else if (num === 3) {
    let flagTemp = true
    while (flagTemp) {
      if (Utils.findPictureTap("确定")) {
        flagTemp = false
      } else {
        Utils.findPictureTap("刷新", 0.8)
      }
    }
    num = 0
  } else {
    num++
    log("检测待突破结界第 " + num + " 次")
  }
  log("总共突破结界数：" + count)
}
